﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using sscore;

namespace DM4
{
    public partial class DM4
    {
        public XMLInfo GetLogList(string usrSessionId, LogType? logType, DateTime? start, DateTime? stop, string usrId, string modCode, MessageType? messType)
        {
            BeginFunction("GetLogList");

            OperationResult userIdOpResult = GetUserIdBySession(usrSessionId);
            XMLInfo xi = new XMLInfo(userIdOpResult.UsrSessionId);

            if (!userIdOpResult)
            {
                WriteLog(userIdOpResult.Message, MessageType.Error);
                return xi;
            }

            string table = "";
            switch (logType)
            {
                case LogType.Daemons: table = Tn.LogDaemons; break;
                case LogType.Error: table = Tn.LogError; break;
                case LogType.General: table = Tn.LogGeneral; break;
                case LogType.System: table = Tn.LogSystem; break;
                case LogType.Test: table = Tn.LogTest; break;
            }


            string sql = String.Format(@"SELECT [lg_id]
                                                ,[lg_time]
                                                ,[lg_mod_code]
                                                ,[lg_message]
                                                ,[lg_mess_type] 
                                                ,case when [users].[usr_fullname] is null then '' else [users].[usr_fullname] end AS [lg_usr_id]
                                            FROM {0} LEFT OUTER JOIN [users] ON [usr_id] = [lg_usr_id]", table);
            List<string> where = new List<string>();

            if (start != null) where.Add(String.Format("{0} >= @timeStart", CnLog.Time));
            if (stop != null) where.Add(String.Format("{0} >= @timeStop", CnLog.Time));
            if (!String.IsNullOrEmpty(usrId) && usrId != DM4Consts.SDefaultNULLId) where.Add(String.Format("{0} = @user", CnLog.UsrId));
            if (!String.IsNullOrEmpty(modCode) && modCode != DM4Consts.SDefaultNULLId) where.Add(String.Format("{0} = @modCode", CnLog.ModCode));

            if (where.Count != 0)
                sql += "WHERE "+String.Join(" and ", where.ToArray());

            WriteDebugLog(sql);

            try
            {
                SqlCommand sel = new SqlCommand(sql, _conn);

                if (start != null)
                    sel.Parameters.AddWithValue("@timeStart", start);
                if (stop != null)
                    sel.Parameters.AddWithValue("@timeStop", stop);
                if (!String.IsNullOrEmpty(usrId) && usrId != DM4Consts.SDefaultNULLId)
                    sel.Parameters.AddWithValue("@user", usrId);
                if (!String.IsNullOrEmpty(modCode) && modCode != DM4Consts.SDefaultNULLId)
                    sel.Parameters.AddWithValue("@modCode", modCode);
                
                using (SqlDataReader reader = sel.ExecuteReader())
                {
                    try
                    {
                        xi.Load(reader, "log");
                    }

                    catch (Exception ex)
                    {
                        WriteLog(ex.Message, MessageType.Error);
                    }
                }
            }

            catch (Exception ex)
            {
                WriteLog(ex.Message, MessageType.Error);
            }

            EndFunction("GetLogList");
            return xi;
        }
    }
}
